package org.ami.onlineschool.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;

import org.ami.onlineschool.model.Assignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

public class UIDefineAssignment extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtAssignemtSubject;
	private Assignment assignment;
	private JEditorPane txtAssignmentBody;

	private String dirPath;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIDefineAssignment dialog = new UIDefineAssignment();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UIDefineAssignment() {
		setTitle("Assignment");
		setBounds(100, 100, 641, 448);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new BorderLayout(0, 0));
			{
				JLabel lblSubject = new JLabel("Subject   ");
				panel.add(lblSubject, BorderLayout.WEST);
			}
			{
				txtAssignemtSubject = new JTextField();
				txtAssignemtSubject.setText("Assignemt Subject");
				panel.add(txtAssignemtSubject, BorderLayout.CENTER);
				txtAssignemtSubject.setColumns(10);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new BorderLayout(0, 0));
			{
				JLabel lblBody = new JLabel("Body");
				panel.add(lblBody, BorderLayout.NORTH);
			}
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1, BorderLayout.SOUTH);
				panel_1.setLayout(new BorderLayout(0, 0));
				{
					JLabel lblNewLabel = new JLabel("Attachments: Copy the files you want to send as attachments in attachments folder.");
					panel_1.add(lblNewLabel, BorderLayout.WEST);
				}
				{
					JPanel panel_2 = new JPanel();
					FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
					flowLayout.setAlignment(FlowLayout.LEFT);
					panel_1.add(panel_2, BorderLayout.CENTER);
					{
						JButton btnAttach = new JButton("Open Attachments Folder");
						btnAttach.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								try {
									String attachmentsFolderPath = dirPath + getAssignment().getAttachmentsFolder();
									ensureAttachmentsFolder(attachmentsFolderPath);
									Runtime.getRuntime().exec("explorer.exe " + attachmentsFolderPath);
								} catch (IOException e1) {
									e1.printStackTrace();
								}

							}
						});
						panel_2.add(btnAttach);
					}
					{
						JLabel lblCopyTheFiles = new JLabel("-");
						panel_2.add(lblCopyTheFiles);
					}
				}
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				panel.add(scrollPane, BorderLayout.CENTER);
				{
					txtAssignmentBody = new JEditorPane();
					txtAssignmentBody.setText("Assignment Body");
					scrollPane.setViewportView(txtAssignmentBody);
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						getAssignment().setSubject(txtAssignemtSubject.getText());
						getAssignment().setMessage(txtAssignmentBody.getText());
						UIDefineAssignment.this.setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						UIDefineAssignment.this.setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	protected void ensureAttachmentsFolder(String attachmentPath) {
		new File(attachmentPath).mkdirs();
	}

	public Assignment getAssignment() {
		return assignment;
	}

	public void setAssignment(Assignment assignment) {
		this.assignment = assignment;
		setTitle(assignment.toString());
		txtAssignemtSubject.setText(assignment.getSubject());
		txtAssignmentBody.setText(assignment.getMessage());
	}

	public String getDirPath() {
		return dirPath;
	}

	public void setDirPath(String dirPath) {
		this.dirPath = dirPath;
	}
	
	
	
}
